package com.atguigu.tingshu.common.service;

import com.atguigu.tingshu.common.pojo.TingShuCorrelationData;
import jakarta.annotation.Resource;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

@Component
public class RabbitmqService {
    @Resource
    private RabbitTemplate rabbitTemplate;
    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    public void sendMqMsg(String exchange,String routingKey,Object msg) {
        sendMqMsg(exchange, routingKey, msg, false, 0);
    }

    public void sendMqMsg(String exchange, String routingKey, Object msg, boolean isRetry, int limit) {
        TingShuCorrelationData tingShuCorrelationData = new TingShuCorrelationData();
        tingShuCorrelationData.setExchange(exchange);
        tingShuCorrelationData.setMsg(msg);
        tingShuCorrelationData.setRoutingKey(routingKey);
        tingShuCorrelationData.setIsRetry(isRetry);
        tingShuCorrelationData.setLimit(limit);
        redisTemplate.opsForValue().set("mq:" + tingShuCorrelationData.getId(), tingShuCorrelationData);
        rabbitTemplate.convertAndSend(exchange, routingKey, msg, tingShuCorrelationData);
    }
}
